/**
 * 
 */
package cn.rengy.tpa.wxmp.lang;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.rengy.tool.core.util.ExceptionPrintUtils;

/**
 */
public class StreamUtils {
	private static Logger logger = LoggerFactory.getLogger(StreamUtils.class);
	/**
	 * stream to string
	 * @param is
	 * @return
	 */
	public static String streamToString(InputStream is){
		ByteArrayOutputStream   baos   =   new   ByteArrayOutputStream(); 
        int i=-1;
        String result=null;
        try {
			while((i=is.read())!=-1){ 
				baos.write(i); 
			}
			result=baos.toString();
		} catch (IOException e) {
			logger.error(ExceptionPrintUtils.getTrace(e));
		}finally{
			try {
				baos.close();
			} catch (IOException e) {
			}
		}
       return   result;
	}
	
	/**
	 * string to stream
	 * @param str
	 * @return
	 */
	public static InputStream strToStream(String str){
		InputStream is = new ByteArrayInputStream(str.getBytes());
		return is;
	}

}
